﻿// Decompiled with JetBrains decompiler
// Type: Unity.Netick.CodeGen.CodeGenUtils
// Assembly: Unity.Netick.CodeGen, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 8B3B8749-9F65-48DA-BE6F-84634E89AC85
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Editor\Unity.Netick.CodeGen.dll

using Mono.Cecil;
using Mono.Cecil.Cil;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Unity.CompilationPipeline.Common.Diagnostics;
using Unity.CompilationPipeline.Common.ILPostProcessing;

#nullable disable
namespace Unity.Netick.CodeGen;

internal static class CodeGenUtils
{
  public const string RuntimeAssemblyName = "Netick";

  public static AssemblyDefinition AssemblyDefinitionFor(
    ICompiledAssembly compiledAssembly,
    out PostProcessorAssemblyResolver assemblyResolver)
  {
    assemblyResolver = new PostProcessorAssemblyResolver(compiledAssembly);
    ReaderParameters readerParameters = new ReaderParameters()
    {
      SymbolStream = (Stream) new MemoryStream(compiledAssembly.InMemoryAssembly.PdbData),
      SymbolReaderProvider = (ISymbolReaderProvider) new PortablePdbReaderProvider(),
      AssemblyResolver = (IAssemblyResolver) assemblyResolver,
      ReflectionImporterProvider = (IReflectionImporterProvider) new PostProcessorReflectionImporterProvider(),
      ReadingMode = (ReadingMode) 1
    };
    AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly((Stream) new MemoryStream(compiledAssembly.InMemoryAssembly.PeData), readerParameters);
    assemblyResolver.AddAssemblyDefinitionBeingOperatedOn(assemblyDefinition);
    return assemblyDefinition;
  }

  public static void AddWarning(this List<DiagnosticMessage> diagnostics, string message)
  {
    diagnostics.AddError((SequencePoint) null, message, false);
  }

  public static void AddError(this List<DiagnosticMessage> diagnostics, string message)
  {
    diagnostics.AddError((SequencePoint) null, message);
  }

  public static void AddError(
    this List<DiagnosticMessage> diagnostics,
    MethodDefinition methodDefinition,
    string message)
  {
    diagnostics.AddError(((IEnumerable<SequencePoint>) methodDefinition.DebugInformation.SequencePoints).FirstOrDefault<SequencePoint>(), message);
  }

  public static void AddError(
    this List<DiagnosticMessage> diagnostics,
    SequencePoint sequencePoint,
    string message,
    bool isError = true)
  {
    diagnostics.Add(new DiagnosticMessage()
    {
      DiagnosticType = isError ? (DiagnosticType) 1 : (DiagnosticType) 2,
      File = sequencePoint?.Document.Url.Replace($"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}", ""),
      Line = sequencePoint != null ? sequencePoint.StartLine : 0,
      Column = sequencePoint != null ? sequencePoint.StartColumn : 0,
      MessageData = " - " + message
    });
  }

  public static AssemblyDefinition AssemblyDefinitionFor(ICompiledAssembly compiledAssembly)
  {
    PostProcessorAssemblyResolver assemblyResolver = new PostProcessorAssemblyResolver(compiledAssembly);
    ReaderParameters readerParameters = new ReaderParameters()
    {
      SymbolStream = (Stream) new MemoryStream(compiledAssembly.InMemoryAssembly.PdbData),
      SymbolReaderProvider = (ISymbolReaderProvider) new PortablePdbReaderProvider(),
      AssemblyResolver = (IAssemblyResolver) assemblyResolver,
      ReflectionImporterProvider = (IReflectionImporterProvider) new PostProcessorReflectionImporterProvider(),
      ReadingMode = (ReadingMode) 1
    };
    AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly((Stream) new MemoryStream(compiledAssembly.InMemoryAssembly.PeData), readerParameters);
    assemblyResolver.AddAssemblyDefinitionBeingOperatedOn(assemblyDefinition);
    return assemblyDefinition;
  }
}
